package com.kimho.localtion.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.inhouse.domain.Inhouse;
import com.kimho.localtion.domain.Localtion;
import com.kimho.localtion.service.LocaltionService;
import com.kimho.util.Util;

@Controller
@RequestMapping("/localtion")
public class LocaltionController {
	@Autowired
	private LocaltionService localtionService;
	
	@RequestMapping("/localtionList")
	public @ResponseBody Map<String, Object> localtionList(){
		Map<String,Object> returnMap = new HashMap<String, Object>();
		List<Localtion> localtions = this.localtionService.localtionList();
		returnMap.put("totalCount", localtions.size());
		returnMap.put("rows", localtions);
		return returnMap;
	}
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> saveOrUpdate(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		String id = request.getParameter("id");
		
		String local_code = request.getParameter("local_code");
		String local_name = request.getParameter("local_name");
		String local_area_code = request.getParameter("local_area_code");
		String local_brief = request.getParameter("local_brief");
		
		try {
			//编辑用户信息
			if(!"".equals(id)) {
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("id", id);
		//		Localtion localtion = localtionSerivce.getInhouse(param);
				
	//			inhouseService.updateInhouse(inhouse, param);
				responseMap.put("success", "true");
				responseMap.put("info", "编辑成功！");
			}
			//新增用户信息
			else {
				Localtion localtion = new Localtion();
				String newid = Util.getUUID();
				localtion.setLocal_id(newid);
				localtion.setLoal_code(Long.parseLong(local_code));
				localtion.setLocal_area_code(Long.parseLong(local_area_code));
				localtion.setLocal_brief(local_brief);
				localtion.setLocal_name(local_name);
			    
			    
				Map<String,Object> param = new HashMap<String,Object>();
				localtionService.insertLocaltion(localtion, param);
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> remove(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String ids = request.getParameter("ids");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("ids", ids);
		try {
			localtionService.deleteLocaltion(param);
			responseMap.put("success", "true");
			responseMap.put("info", "删除成功！");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
}
}
